package com.demo.spring_demo.controller.song;

import com.demo.spring_demo.mapper.SongMapper;
import com.demo.spring_demo.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 歌曲控制层
 */
@RestController
@RequestMapping("/song")
public class SongController {
    @Autowired
    SongMapper songMapper;

    /**
     * 1、分页模糊查找歌曲
     * @param pageInfo
     * @return
     */
    @PostMapping("/pageSelect")
    public Result pageSelect(@RequestBody PageInfo<Song> pageInfo) {
        List<Song> songList = null;
        int count = 0;
        if ((count = songMapper.getCount(pageInfo.getSong())) > 0 && (songList = songMapper.getSong(pageInfo.getSong(), pageInfo.getPageIndex() * pageInfo.getPageSize(), pageInfo.getPageSize())) != null) {
            return new Result(200, new PageInfo<Song>(pageInfo.getPageIndex(), pageInfo.getPageSize(), songList, count));
        } else {
            return new Result(500, "信息不存在！");
        }
    }

    /**
     * 2、添加单曲入库
     * @param song
     * @return
     */
    @PostMapping("/addSong")
    public Result addSong(@RequestBody Song song) {
        if(song != null && songMapper.addSong(song) > 0){
            return new Result(200, song, "success！");
        }else {
            return new Result(500, "error");
        }
    }


    /**
     * 3、修改歌曲
     * @param song
     * @return
     */
    @PostMapping("/updateSong")
    public Result updateSong(@RequestBody Song song){
        if(song != null && songMapper.updateSong(song) > 0) {
            return new Result(200,"success");
        }
        return new Result(500,"error");
    }

    /**
     * 4、删除歌曲
     * @param
     * @return
     */
    @GetMapping("/deleteSong/{id}")
    public Result deleteSong(@PathVariable int id){
        if(id > 0 && songMapper.deleteSong(id) > 0) {
            return new Result(200,"success");
        }
        return new Result(500,"error");
    }


    /**
     * 5、歌单入库
     * @param songList
     * @return
     */
    @PostMapping("/addSongList")
    public Result addSongList(@RequestBody List<Song> songList){
       if(songList != null){
           int i = songMapper.addSongList(songList);
           return new Result(200,i,"success");
       }
        return new Result(500,"error");
    }
}
